{% set pypi_name = 'masakari-monitors' %}
{% set source = fetch_source('https://tarballs.openstack.org/masakari-monitors/masakari-monitors-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}

Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        Monitors for Virtual Machine High Availability (VMHA) service in OpenStack
License:        {{ license('Apache-2.0') }}
Group:          Development/Libraries
URL:            https://docs.openstack.org/masakari-monitors
# basename filter prevents OBS from downloading the source itself which would
# fetch from a stale cache hashed by non-changing URL.
Source0:        {{ source|basename }}
Source1:        openstack-masakari-hostmonitor.service
Source2:        openstack-masakari-instancemonitor.service
Source3:        openstack-masakari-processmonitor.service
Source4:        openstack-masakari-monitors.tmpfiles
Source5:        openstack-masakari-introspectiveinstancemonitor.service
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('eventlet') }}
BuildRequires:  {{ py3('keystoneauth1') }}
BuildRequires:  {{ py3('libvirt-python') }}
BuildRequires:  {{ py3('python-openstackclient') }}
BuildRequires:  {{ py3('openstacksdk') }}
BuildRequires:  {{ py3('oslo.concurrency') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.db') }}
BuildRequires:  {{ py3('oslo.i18n') }}
BuildRequires:  {{ py3('oslo.log') }}
BuildRequires:  {{ py3('oslo.middleware') }}
BuildRequires:  {{ py3('oslo.policy') }}
BuildRequires:  {{ py3('oslo.privsep') }}
BuildRequires:  {{ py3('oslo.service') }}
Requires:       python3-masakari-monitors = %{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  systemd-rpm-macros
%{?systemd_requires}
%else
BuildRequires:  systemd
Requires(post): systemd
Requires(postun): systemd
Requires(preun): systemd
%endif

%description
Monitors for the Masakari service for Virtual Machine
High Availability (VMHA) service in OpenStack.

System package - %{name}

%package        doc
Summary:        Documentation for OpenStack Masakari Service
BuildRequires:  {{ py3('Sphinx') }}
BuildRequires:  {{ py3('openstackdocstheme') }}
BuildRequires:  {{ py3('sphinxcontrib-httpdomain') }}
BuildRequires:  {{ py3('sphinxcontrib-pecanwsme') }}

%description    doc
Monitors for the Masakari service for Virtual Machine
High Availability (VMHA) service in OpenStack.

This package contains the documentation.

%package -n     python3-{{ pypi_name }}
Summary:        masakari-monitors Python libraries
Requires:       {{ py3('Babel') }}
Requires:       {{ py3('jsonschema') }}
Requires:       {{ py3('keystoneauth1') }}
Requires:       {{ py3('openstacksdk') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.i18n') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('oslo.middleware') }}
Requires:       {{ py3('oslo.privsep') }}
Requires:       {{ py3('oslo.service') }}
Requires:       {{ py3('oslo.utils') }}
Requires:       {{ py3('pbr') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('stevedore') }}

%description -n python3-{{ pypi_name }}
Masakari is a service providing high availability for VMs in OpenStack.

This package contains the Python libraries for the monitoring components.

%package -n openstack-masakari-hostmonitor
Summary:        Host monitoring component of OpenStack Masakari service
Requires:       %{name} = %{version}-%{release}

%description -n openstack-masakari-hostmonitor
This component of the Masakari service for VM HA in OpenStack is
responsible for monitoring the health of nova-compute hypervisors.

%package -n openstack-masakari-instancemonitor
Summary:        Instance monitoring component of OpenStack Masakari service
Requires:       %{name} = %{version}-%{release}

%description -n openstack-masakari-instancemonitor
This component of the Masakari service for VM HA in OpenStack is
responsible for monitoring the health of individual VM instances.

%package -n openstack-masakari-introspectiveinstancemonitor
Summary:        Instance monitoring component of OpenStack Masakari service
Requires:       %{name} = %{version}-%{release}

%description -n openstack-masakari-introspectiveinstancemonitor
This component of the Masakari service for VM HA in OpenStack is
responsible for monitoring the health of individual VM instances.

%package -n openstack-masakari-processmonitor
Summary:        Process monitoring component of OpenStack Masakari service
Requires:       %{name} = %{version}-%{release}

%description -n openstack-masakari-processmonitor
This component of the Masakari service for VM HA in OpenStack is
responsible for monitoring the health of processes which need to stay
running in order for the compute plane to be functional and highly
available.

%prep
%autosetup -p1 -n {{ pypi_name }}-{{upstream_version}}
%py_req_cleanup

%build
%{py3_build}
PYTHONPATH=. oslo-config-generator --config-file etc/masakarimonitors/masakarimonitors-config-generator.conf

PBR_VERSION={{ upstream_version }} %sphinx_build -b html doc/source doc/build/html
# remove the sphinx-build leftovers
rm -rf doc/build/html/.{doctrees,buildinfo}

%install
%{py3_install}

mkdir -p %{buildroot}%{_sysconfdir}/masakarimonitors/
mkdir -p %{buildroot}%{_localstatedir}/log/masakari-monitors

#systemd unitfiles
install -p -D -m 644 %SOURCE1 %{buildroot}%{_unitdir}/openstack-masakari-hostmonitor.service
install -p -D -m 644 %SOURCE2 %{buildroot}%{_unitdir}/openstack-masakari-instancemonitor.service
install -p -D -m 644 %SOURCE3 %{buildroot}%{_unitdir}/openstack-masakari-processmonitor.service
install -p -D -m 644 %SOURCE5 %{buildroot}%{_unitdir}/openstack-masakari-introspectiveinstancemonitor.service

install -p -D -m 644 %SOURCE4 %{buildroot}/%{_tmpfilesdir}/openstack-masakari-monitors.conf

install -p -D -m 640 etc/masakarimonitors/masakarimonitors.conf.sample %{buildroot}%{_sysconfdir}/masakarimonitors/masakarimonitors.conf
install -p -D -m 640 etc/masakarimonitors/hostmonitor.conf.sample %{buildroot}%{_sysconfdir}/masakarimonitors/hostmonitor.conf
install -p -D -m 640 etc/masakarimonitors/processmonitor.conf.sample %{buildroot}%{_sysconfdir}/masakarimonitors/processmonitor.conf
install -p -D -m 640 etc/masakarimonitors/process_list.yaml.sample %{buildroot}%{_sysconfdir}/masakarimonitors/process_list.yaml
install -p -D -m 640 etc/masakarimonitors/proc.list.sample %{buildroot}%{_sysconfdir}/masakarimonitors/proc.list
chmod +x %{buildroot}%{_bindir}/masakari-*

%pre
%openstack_pre_user_group_create masakari masakari

%post
%tmpfiles_create %{_tmpfilesdir}/openstack-masakari-monitors.conf

%post -n openstack-masakari-hostmonitor
%systemd_post openstack-masakari-hostmonitor.service

%preun -n openstack-masakari-hostmonitor
%systemd_preun openstack-masakari-hostmonitor.service

%postun -n openstack-masakari-hostmonitor
%systemd_postun_with_restart openstack-masakari-hostmonitor.service

%post -n openstack-masakari-instancemonitor
%systemd_post openstack-masakari-instancemonitor.service

%preun -n openstack-masakari-instancemonitor
%systemd_preun openstack-masakari-instancemonitor.service

%postun -n openstack-masakari-instancemonitor
%systemd_postun_with_restart openstack-masakari-instancemonitor.service

%post -n openstack-masakari-processmonitor
%systemd_post openstack-masakari-processmonitor.service

%preun -n openstack-masakari-processmonitor
%systemd_preun openstack-masakari-processmonitor.service

%postun -n openstack-masakari-processmonitor
%systemd_postun_with_restart openstack-masakari-processmonitor.service

%files
%license LICENSE
%dir %{_sysconfdir}/masakarimonitors
%config(noreplace) %attr(-, masakari, masakari) %{_sysconfdir}/masakarimonitors/masakarimonitors.conf
%dir %attr(750, masakari, masakari) %{_localstatedir}/log/masakari-monitors
%_tmpfilesdir/openstack-masakari-monitors.conf

%files -n openstack-masakari-hostmonitor
%config(noreplace) %attr(-, masakari, masakari) %{_sysconfdir}/masakarimonitors/hostmonitor.conf
%{_bindir}/masakari-hostmonitor
%{_unitdir}/openstack-masakari-hostmonitor.service

%files -n openstack-masakari-instancemonitor
%{_bindir}/masakari-instancemonitor
%{_unitdir}/openstack-masakari-instancemonitor.service

%files -n openstack-masakari-introspectiveinstancemonitor
%{_bindir}/masakari-introspectiveinstancemonitor
%{_unitdir}/openstack-masakari-introspectiveinstancemonitor.service

%files -n openstack-masakari-processmonitor
%{_bindir}/masakari-processmonitor
%{_unitdir}/openstack-masakari-processmonitor.service
%config(noreplace) %attr(-, masakari, masakari) %{_sysconfdir}/masakarimonitors/processmonitor.conf
%config(noreplace) %attr(-, masakari, masakari) %{_sysconfdir}/masakarimonitors/process_list.yaml
%config(noreplace) %attr(-, masakari, masakari) %{_sysconfdir}/masakarimonitors/proc.list

%files doc
%license LICENSE
%doc doc/build/html

%files -n python3-{{ pypi_name }}
%license LICENSE
%{python3_sitelib}/masakari*

%changelog
